package cn.com.thingo.shyintelligent.ui.fragment;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.th.opensdk.openapi.SceneGroupInfo;
import com.th.opensdk.openapi.SceneInfo;
import com.th.opensdk.openapi.THOpenSDK;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.thingo.shyintelligent.R;
import cn.com.thingo.shyintelligent.app.App;
import cn.com.thingo.shyintelligent.base.RootFragment;
import cn.com.thingo.shyintelligent.base.contract.ScenesContract;
import cn.com.thingo.shyintelligent.model.bean.CommonBean;
import cn.com.thingo.shyintelligent.model.bean.SceneInfoBean;
import cn.com.thingo.shyintelligent.presenter.ScenesPresenter;
import cn.com.thingo.shyintelligent.ui.activity.MessageActivity;
import cn.com.thingo.shyintelligent.ui.activity.PersonalActivity;
import cn.com.thingo.shyintelligent.ui.adapter.ScenesAdapter;
import cn.com.thingo.shyintelligent.util.SPUtils;
import cn.com.thingo.shyintelligent.util.ToastUtil;

public class ScenesFragment extends RootFragment<ScenesPresenter> implements ScenesContract.View, ScenesAdapter.OnItemClickListener {
    @BindView(R.id.scenes_personal)
    ImageView ivPersonal;
    @BindView(R.id.scenes_message)
    ImageView ivMessage;
    @BindView(R.id.scenes_add_determine)
    TextView tvAddDetermine;
    @BindView(R.id.scenes_list_rv)
    RecyclerView rvList;
    private THOpenSDK thOpenSDK;
    private ScenesAdapter adapter;
    private List<SceneInfoBean> sceneList = new ArrayList<>();


    public ScenesFragment() {
    }

    public static ScenesFragment newInstance() {
        return new ScenesFragment();
    }

    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_scenes;
    }

    @Override
    protected void initEventAndData() {
        super.initEventAndData();
        thOpenSDK = THOpenSDK.getInstance();
        initView();

    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    private void initData() {
        sceneList.clear();
        if (thOpenSDK == null) {
            thOpenSDK = THOpenSDK.getInstance();
        }
        List<SceneGroupInfo> sceneGroupList = thOpenSDK.getSceneGroupList();
        List<SceneInfo> sceneInfoList = new ArrayList<>();
        if (sceneGroupList != null && sceneGroupList.size() > 0) {
            for (int i = 0; i < sceneGroupList.size(); i++) {
                sceneInfoList.addAll(sceneGroupList.get(i).getSceneList());
            }
            if (sceneInfoList.size() > 0) {
                for (int i = 0; i < sceneInfoList.size(); i++) {
                    if (!sceneInfoList.get(i).getSceneName().startsWith("窗户-")) {
                        sceneList.add(new SceneInfoBean(sceneInfoList.get(i)));
                    }
                }
            }
        }
        adapter.notifyDataSetChanged();
    }

    private void initView() {
        rvList.setLayoutManager(new GridLayoutManager(mContext, 3));
        adapter = new ScenesAdapter(sceneList, mContext);
        adapter.setListener(this);
        rvList.setAdapter(adapter);

    }

    @Override
    public void onItemClick(int position) {
        final SceneInfo sceneInfo = sceneList.get(position).getSceneInfo();
        if (App.getInstance().isScenesStatus()) {
            for (int i = 0; i < sceneList.size(); i++) {
                sceneList.get(i).setStart(i == position);
            }
            adapter.notifyDataSetChanged();
            //场景开关控制
            App.getInstance().setScenesStatus(false);
            long controlId = sceneInfo.getControlId();
            App.getInstance().setCurrentScenesId(controlId);
            new Thread() {
                @Override
                public void run() {
                    super.run();
                    sceneInfo.run();
                }
            }.start();
        } else {
            ToastUtil.shortShow("有其他场景的操作正在部署中...");
        }
    }

    @OnClick({R.id.scenes_personal, R.id.scenes_message, R.id.scenes_add_determine})
    public void onClick(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.scenes_message:
                intent.setClass(mContext, MessageActivity.class);
                startActivity(intent);
                break;
            case R.id.scenes_personal:
                intent.setClass(mContext, PersonalActivity.class);
                startActivity(intent);
                break;
            case R.id.scenes_add_determine:
                String name = tvAddDetermine.getText().toString().trim();
                if (TextUtils.equals(name, "添加")) {
                    tvAddDetermine.setText("确认");
                    String selectPositions = SPUtils.newInstance().getString("scenes_num".concat(App.getDataBean().getNumber()));
                    if (selectPositions != null) {
                        String[] split = selectPositions.split(",");
                        for (int i = 0; i < sceneList.size(); i++) {
                            for (String aSplit : split) {
                                if (TextUtils.equals(String.valueOf(i), aSplit)) {
                                    sceneList.get(i).setSelectorStauts(true);
                                    break;
                                } else {
                                    sceneList.get(i).setSelectorStauts(false);
                                }
                            }
                        }
                    }
                    adapter.setCanStart(false);
                    adapter.setSelect(true);
                    adapter.setStart(false);
                    adapter.notifyDataSetChanged();
                } else {
                    tvAddDetermine.setText("添加");
                    adapter.setSelect(false);
                    String selectPositions = adapter.getSelectPositions();
                    if (selectPositions != null) {
                        SPUtils.newInstance().put("scenes_num".concat(App.getDataBean().getNumber()), selectPositions);
                    } else {
                        SPUtils.newInstance().remove("scenes_num".concat(App.getDataBean().getNumber()));
                    }
                    adapter.setCanStart(true);
                    adapter.setStart(true);
                    adapter.notifyDataSetChanged();
                }
                break;
        }

    }

    public void messageCount() {
        mPresenter.messageCount(SPUtils.newInstance().getString(SPUtils.LOGIN_TOKEN));
    }

    @Override
    public void viewMessageCount(CommonBean commonBean) {
        int count = commonBean.getCount();
        if (count == 0) {
            ivMessage.setImageResource(R.mipmap.message);
        } else {
            ivMessage.setImageResource(R.mipmap.message_count);

        }
    }
}
